package com.shop.loan.service.impl;

import com.shop.loan.mapper.EruptUserByMybatisMapper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.entity.EruptUserByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.model.enums.ServiceType;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.service.EruptUserDataService;
import com.shop.loan.service.MsgService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EruptUserDataServiceImpl implements EruptUserDataService {

    @Resource
    RedisUtil redisUtil;

    @Resource
    EruptUserByMybatisMapper eruptUserByMybatisMapper;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    MsgService msgService;

    /**
     * 分配客服
     *
     * @param serviceType
     * @param userId
     * @return
     */
    public Long allotServiceByBefore(ServiceType serviceType, Long userId) {
        String code = serviceType.getType();
        List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(code);
        if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
            return null;
        }
        String key = RedisCache.service_allot_index + code;
        String valueStr = redisUtil.get(key);
        Long index = (long) eruptUserByMybatisList.size();
        if (StringUtils.hasText(valueStr)) {
            index = Long.parseLong(valueStr);
        } else {
            redisUtil.incrBy(key, eruptUserByMybatisList.size());
        }
        long number = index % eruptUserByMybatisList.size();
        EruptUserByMybatis eruptUserByMybatis = eruptUserByMybatisList.get((int) number);
        redisUtil.incr(key);
        if (serviceType.getType().equals(ServiceType.before_service.getType())) {
            UserByMybatis user = new UserByMybatis();
            user.setId(userId);
            user.setServiceUserId(eruptUserByMybatis.getId());
            userByMybatisMapper.updateByPrimaryKeySelective(user);
        }
        return eruptUserByMybatis.getId();
    }


    /**
     * 分配放款/催收/售后客服
     *
     * @param orderByMybatisList
     * @param serviceType
     */
    public void allotServiceByList(List<LoanOrderByMybatis> orderByMybatisList, ServiceType serviceType) {
        List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(serviceType.getType());
        if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
            return;
        }
        String key = RedisCache.service_allot_index + serviceType.getType();
        for (LoanOrderByMybatis loanOrderByMybatis : orderByMybatisList) {
            if (serviceType.getType().equals(ServiceType.collection_service.getType())) {
                if (loanOrderByMybatis.getCollectionServiceId() != null) {
                    continue;
                }
            } else if (serviceType.getType().equals(ServiceType.after_service.getType())) {
                if (loanOrderByMybatis.getAfterServiceId() != null) {
                    continue;
                }
            } else if (serviceType.getType().equals(ServiceType.loan_service.getType())) {
                if (loanOrderByMybatis.getLoanServiceId() != null) {
                    continue;
                }
            } else {
                return;
            }
            String valueStr = redisUtil.get(key);
            Long index = (long) eruptUserByMybatisList.size();
            if (StringUtils.hasText(valueStr)) {
                index = Long.parseLong(valueStr);
            } else {
                redisUtil.incrBy(key, eruptUserByMybatisList.size());
            }
            long number = index % eruptUserByMybatisList.size();
            EruptUserByMybatis eruptUserByMybatis = eruptUserByMybatisList.get((int) number);
            redisUtil.incr(key);

            LoanOrderByMybatis update = new LoanOrderByMybatis();
            update.setId(loanOrderByMybatis.getId());
            if (serviceType.getType().equals(ServiceType.collection_service.getType())) {
                update.setCollectionServiceId(eruptUserByMybatis.getId());
            } else if (serviceType.getType().equals(ServiceType.after_service.getType())) {
                update.setAfterServiceId(eruptUserByMybatis.getId());
            } else if (serviceType.getType().equals(ServiceType.loan_service.getType())) {
                update.setLoanServiceId(eruptUserByMybatis.getId());
            }
            loanOrderByMybatisMapper.updateByPrimaryKeySelective(update);

            msgService.newMsgByAdmin(eruptUserByMybatis.getId());
        }
    }


    /**
     * 校验客服权限
     *
     * @param serviceType
     * @return
     */
    public Boolean checkServiceAuth(ServiceType serviceType, Long eruptUserId) {
        EruptUserByMybatis eruptUserByMybatis = eruptUserByMybatisMapper.selectByPrimaryKey(eruptUserId);
        if (eruptUserByMybatis.getIsAdmin()) {
            return true;
        }
        String code = serviceType.getType();
        List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(code);
        if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
            return false;
        }

        for (EruptUserByMybatis userByMybatis : eruptUserByMybatisList) {
            if (userByMybatis.getId() == eruptUserId.intValue()) {
                return true;
            }
        }
        return false;
    }


    /**
     * 校验是否是渠道
     * @param eruptUserId
     * @return
     */
    public Boolean checkChannelAuth(Long eruptUserId) {
        String code = "channel";
        List<EruptUserByMybatis> eruptUserByMybatisList = eruptUserByMybatisMapper.selectByRoleCode(code);
        if (eruptUserByMybatisList == null || eruptUserByMybatisList.size() <= 0) {
            return false;
        }
        for (EruptUserByMybatis userByMybatis : eruptUserByMybatisList) {
            if (userByMybatis.getId() == eruptUserId.intValue()) {
                return true;
            }
        }
        return false;
    }

}
